/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.uniscol.siecle.dao.impl;

import fr.uniscol.dao.UniscolDaoSupport;

import static fr.uniscol.dao.impl.SQLNames.*;
import fr.uniscol.model.BaseAnneeScolaire;
import fr.uniscol.siecle.model.CorrespondanceSiecleSts;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Christophe
 */
@Repository("correspondancesSiecleStsDao")
public class CorrespondanceSIECLESTSDaoImpl extends UniscolDaoSupport {
    
    public CorrespondanceSiecleSts findByIdSiecle(BaseAnneeScolaire base, String idSiecle, String type){
        
        List<CorrespondanceSiecleSts> correspondances = getJdbcTemplate().query("SELECT * FROM " + TABLE_CORRESPONDANCES_SIECLE_STS + " WHERE baseid=? AND id_siecle=? AND type=? ", 
            new RowMapper<CorrespondanceSiecleSts>(){

                @Override
                public CorrespondanceSiecleSts mapRow(ResultSet rs, int rowNum) throws SQLException {
                    
                    CorrespondanceSiecleSts cor = new CorrespondanceSiecleSts();
                    
                    cor.setId(rs.getLong("CORRESPONDANCEID"));
                    cor.setIdSiecle(rs.getString("ID_SIECLE"));
                    cor.setIdSts(rs.getString("ID_STS"));
                    cor.setType(rs.getString("TYPE"));
                    
                    return cor;
                }
                
            },
            new Object[]{
               base.getId(),
               idSiecle,
               type
            });
        
        if(correspondances.isEmpty()){
            return null;
        }else{
            return correspondances.get(0);
        }
    }
    
    public CorrespondanceSiecleSts findByIdSts(BaseAnneeScolaire base, String idSts, String type){
        
        List<CorrespondanceSiecleSts> correspondances = getJdbcTemplate().query("SELECT * FROM " + TABLE_CORRESPONDANCES_SIECLE_STS + " WHERE baseid=? AND id_sts=? AND type=? ", 
            new RowMapper<CorrespondanceSiecleSts>(){

                @Override
                public CorrespondanceSiecleSts mapRow(ResultSet rs, int rowNum) throws SQLException {
                    
                    CorrespondanceSiecleSts cor = new CorrespondanceSiecleSts();
                    
                    cor.setId(rs.getLong("CORRESPONDANCEID"));
                    cor.setIdSiecle(rs.getString("ID_SIECLE"));
                    cor.setIdSts(rs.getString("ID_STS"));
                    cor.setType(rs.getString("TYPE"));
                    
                    return cor;
                }
                
            },
            new Object[]{
               base.getId(),
               idSts,
               type
            });
        
        if(correspondances.isEmpty()){
            return null;
        }else{
            return correspondances.get(0);
        }
    }
}
